package b1.mobile.blackberry.ui.component;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class SearchBarField extends HorizontalFieldManager {
	private SingleLineEditField _editField;
	private ImageButton _btn;
	private int _btnWidth = 0;
	private int _btnHeight = 0;
	
	public SearchBarField() {
		super(USE_ALL_WIDTH | USE_ALL_HEIGHT);

		this.setBackground(BackgroundFactory.createSolidBackground(0x00B0B0B0));
		
		_editField = new SingleLineEditField(Field.FIELD_VCENTER);
		add(_editField);

		Bitmap btnBg = Bitmap.getBitmapResource("search_1.png");
		_btn = new ImageButton(Bitmap.getBitmapResource("search_1.png"), Bitmap.getBitmapResource("search_2.png"));
		_btnWidth = btnBg.getWidth();
		_btnHeight = btnBg.getHeight();
		add(_btn);
	}

	public int getPreferredHeight() {
		// fixed height
		return 55;
	}

	protected void sublayout(int maxWidth, int maxHeight) {
		setPositionChild(_editField, 10, 10);
		layoutChild(_editField, maxWidth - _btnWidth - 30, 35);
		setPositionChild(_btn, maxWidth - _btnWidth - 10, (int)Math.floor(((getPreferredHeight() - _btnHeight) / 2)));
		layoutChild(_btn, _btnWidth, _btnHeight);
//		super.sublayout(maxWidth, getPreferredHeight());
		setExtent(maxWidth, getPreferredHeight());
	}

	public SingleLineEditField getEditField() {
		return _editField;
	}

	public ImageButton getSearchButton() {
		return _btn;
	}

}
